<?php
//**************************************************
// Add theme support for feeds
//**************************************************
add_theme_support( 'automatic-feed-links' );

//**************************************************
// Override default post type labels
//**************************************************
function change_post_menu_label() {
	global $menu;
	global $submenu;
	$menu[5][0] = Rapporter;
	$submenu['edit.php'][5][0] = 'Rapporter';
	$submenu['edit.php'][10][0] = 'Lägg till';
	$submenu['edit.php'][16][0] = 'Nyckelord';
	echo '';
}
function change_post_object_label() {
	global $wp_post_types;
	$labels = &$wp_post_types['post']->labels;
	$labels->name = 'Rapporter';
	$labels->singular_name = 'Rapport';
	$labels->add_new = 'Lägg till rapport';
	$labels->add_new_item = 'Lägg till rapport';
	$labels->edit_item = 'Redigera rapport';
	$labels->new_item = 'Rapport';
	$labels->view_item = 'Visa rapport';
	$labels->search_items = 'Sök rapporter';
	$labels->not_found = 'Inga rapporter hittades';
	$labels->not_found_in_trash = 'Inga rapporter hittades i papperskorgen';
}
add_action( 'init', 'change_post_object_label' );
add_action( 'admin_menu', 'change_post_menu_label' );

//**************************************************
// Override default tag and category labels
//**************************************************
add_action( 'init', 'wps_trac_tax_init');
function wps_trac_tax_init()
{
    global $wp_taxonomies;

    $wp_taxonomies['post_tag']->labels = (object)array(
        'name' => 'Nyckelord',
        'singular_name' => 'Nyckelord',
        'search_items' => 'Sök nyckelord',
        'popular_items' => 'Populära nyckelord',
        'all_items' => 'Alla nyckelord',
        'parent_item' => null, // Tags aren't hierarchical
        'parent_item_colon' => null,
        'edit_item' => 'Redigera nyckelord',
        'update_item' => 'Uppdatera nyckelord',
        'add_new_item' => 'Lägg till nyckelord',
        'new_item_name' => 'Namn på nytt nyckelord',
        'separate_items_with_commas' => 'Separera flera nyckelord med kommatecken',
        'add_or_remove_items' => 'Lägg till/ta bort nyckelord',
        'choose_from_most_used' => 'Välj från mest använda nyckelorden',
    );
	
    $wp_taxonomies['category']->labels = (object)array(
		'name' => 'Komponenter',
		'singular_name' => 'Komponent',
		'search_items' => 'Sök komponent',
		'all_items' => 'Alla komponenter',
		'parent_item' => 'Huvudkomponent',
		'parent_item_colon' => 'Huvudkomponent:',
		'edit_item' => 'Redigera komponent', 
		'update_item' => 'Uppdatera komponenter',
		'add_new_item' => 'Lägg till komponent',
		'new_item_name' => 'Namn för ny komponent',
		'menu_name' => 'Komponent',
    );

    $wp_taxonomies['post_tag']->label = 'Keywords';
    $wp_taxonomies['category']->label = 'Komponenter';
}

//**************************************************
// Create trac taxonomies
//**************************************************
add_action( 'init', 'build_trac_taxonomies', 0 );  
   
function build_trac_taxonomies() {  
register_taxonomy(
		'typ', 'post', array(
		'hierarchical' => true,
		'label' => 'Typ',
		'query_var' => true,
		'rewrite' => array( 
			'slug' => 'typ' ) 
));

register_taxonomy(
		'prioritet', 'post', array( 
		'hierarchical' => true,
		'label' => 'Prioritet',
		'query_var' => true, 
		'rewrite' => array( 
			'slug' => 'prioritet' ) 
));

register_taxonomy( 
		'component_version', 'post', array(
		'hierarchical' => false,
		'label' => 'Version',
		'query_var' => true,
		'rewrite' => array( 
			'slug' => 'version' ) 
));

register_taxonomy( 
		'milestone', 'post', array(
		'hierarchical' => false,
		'label' => 'Målversion',
		'query_var' => true,
		'rewrite' => array( 
			'slug' => 'malversion' ) 
));

register_taxonomy( 
		'allvarlighetsgrad', 'post', array(
		'hierarchical' => true,
		'label' => 'Allvarlighetsgrad',
		'query_var' => true,
		'rewrite' => array( 
			'slug' => 'allvarlighetsgrad' ) 
));

register_taxonomy( 
		'ticket_status', 'post', array(
		'hierarchical' => true,
		'label' => 'Status',
		'query_var' => true,
		'rewrite' => array( 
			'slug' => 'status' ) 
));

register_taxonomy( 
		'ticket_assign', 'post', array(
		'hierarchical' => true,
		'label' => 'Tilldelat',
		'query_var' => true,
		'rewrite' => array( 
			'slug' => 'tilldelat' ) 
));

}

//**************************************************
// Form antispam question
//**************************************************
function encode_str ($input)//converts the input into Ascii HTML
{
    for ($i = 0; $i < strlen($input); $i++) {
         $output .= "&#".ord($input[$i]).';';
    }
    return $output;
}
function mathCaptcha(){//generates a captcha for the form
	$first_number=mt_rand(1, 94);//first operation number
	$second_number=mt_rand(1, 5);//second operation number
	$_SESSION["mathCaptcha"]=($first_number+$second_number);//operation result
	$operation=" <strong>".encode_str($first_number ." + ". $second_number)."</strong> = ";//operation codifieds
	echo "Vad är".$operation;		
}

//**************************************************
// Create bread crumb function
//**************************************************
function trac_breadcrumbs() {
 
  $delimiter = '&gt;';
  $name = 'Tracker'; //text for the 'Home' link
  $currentBefore = '<span class="current">';
  $currentAfter = '</span>';
   
    global $post;
    $home = get_bloginfo('url');
    echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';
 
    if ( is_category() ) {
      global $wp_query;
      $cat_obj = $wp_query->get_queried_object();
      $thisCat = $cat_obj->term_id;
      $thisCat = get_category($thisCat);
      $parentCat = get_category($thisCat->parent);
      if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
      echo $currentBefore . 'Arkiv för komponent &#39;';
      single_cat_title();
      echo '&#39;' . $currentAfter;
 
    } elseif ( is_day() ) {
      echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
      echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('d') . $currentAfter;
 
    } elseif ( is_month() ) {
      echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('F') . $currentAfter;
 
    } elseif ( is_year() ) {
      echo $currentBefore . get_the_time('Y') . $currentAfter;
 
    } elseif ( is_single() && !is_attachment() ) {
	  if(is_tax())
		{
		$post_type = get_post_type_object(get_post_type());
		
		$slug = $post_type->rewrite;
		echo '<a href="'.$home.'/'.$slug['slug'].'/">';
		echo $post_type->labels->name;
		echo '</a> ';
		echo $delimiter . ' ';
		
		echo $currentBefore;
		the_title(); 
		echo $currentAfter;
		
		} else {
		
		$cat = get_the_category();
		$cat = $cat[0];
		//echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
		echo $currentBefore;
		echo 'Rapport #'.get_the_ID();
		echo $currentAfter;
		}
		
		} elseif ( !is_single() && !is_page() && !is_search() && get_post_type() )
		{
		
		$post_type = get_post_type_object(get_post_type());
		echo $currentBefore;
		echo $post_type->labels->name;
		echo $currentAfter;
 
    } elseif ( is_attachment() ) {
      $parent = get_post($post->post_parent);
      $cat = get_the_category($parent->ID); $cat = $cat[0];
      echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
      echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a> ' . $delimiter . ' ';
      echo $currentBefore;
      the_title();
      echo $currentAfter;
 
    } elseif ( is_page() && !$post->post_parent ) {
      echo $currentBefore;
      the_title();
      echo $currentAfter;
 
    } elseif ( is_page() && $post->post_parent ) {
      $parent_id  = $post->post_parent;
      $breadcrumbs = array();
      while ($parent_id) {
        $page = get_page($parent_id);
        $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
        $parent_id  = $page->post_parent;
      }
      $breadcrumbs = array_reverse($breadcrumbs);
      foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
      echo $currentBefore;
      the_title();
      echo $currentAfter;
 
    } elseif ( is_search() ) {
      echo $currentBefore . 'Sökresultat för &#39;' . get_search_query() . '&#39;' . $currentAfter;
 
    } elseif ( is_tag() ) {
      echo $currentBefore . 'Innehåll med nyckelord &#39;';
      single_tag_title();
      echo '&#39;' . $currentAfter;
 
    } elseif ( is_author() ) {
       global $author;
      $userdata = get_userdata($author);
      echo $currentBefore . 'Rapporter postade av ' . $userdata->display_name . $currentAfter;
 
    } elseif ( is_404() ) {
      echo $currentBefore . 'Fel 404' . $currentAfter;
    }
 
    if ( get_query_var('paged') ) {
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
      echo 'Sida' . ' ' . get_query_var('paged');
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
    }
 
  }

//**************************************************
// Callback function for comments and pingbacks
//**************************************************
if ( ! function_exists( 'trac_comments' ) ) :

function trac_comments( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case '' :
	?>
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
		<div id="comment-<?php comment_ID(); ?>">
		<div class="comment-author vcard">
			<?php echo get_avatar( $comment, 40 ); ?>
			<?php printf( __( 'postat av %s' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
		</div><!-- .comment-author .vcard -->
		<?php if ( $comment->comment_approved == '0' ) : ?>
			<em>Din kommentar inväntar granskning.</em>
			<br />
		<?php endif; ?>

		<div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
			<?php
				printf( __( '%1$s kl. %2$s' ), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Redigera)' ), ' ' );
			?>
		</div><!-- .comment-meta .commentmetadata -->

		<div class="comment-body"><?php comment_text(); ?></div>

		<div class="reply">
			<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
		</div><!-- .reply -->
	</div><!-- #comment-##  -->

	<?php
			break;
		case 'pingback'  :
		case 'trackback' :
	?>
	<li class="post pingback">
		<p><?php _e( 'Pingback:' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __('(Redigera)'), ' ' ); ?></p>
	<?php
			break;
	endswitch;
}
endif;

//**************************************************
// Fix for WordPress taxonomy dropdown bug
//**************************************************
function get_terms_dropdown($taxonomies, $args){
        $myterms = get_terms($taxonomies, $args);
        $output ="<select name='typ'>";
        foreach($myterms as $term){
                $root_url = get_bloginfo('url');
                $term_taxonomy=$term->taxonomy;
                $term_slug=$term->slug;
                $term_name =$term->name;
                $link = $term_slug;
                $output .="<option value='-1'>- Typ -</option>";
                $output .="<option value='".$link."'>".$term_name."</option>";
        }
        $output .="</select>";
return $output;
}

//**************************************************
// Send mail to user when a ticket is published
//**************************************************
//function _trac_NotifyAuthor( $new_status, $old_status, $post_id )
//{
//   global $wpdb;
//   $post = get_post($post_id);
//   $author = get_userdata($post->post_author);
//
//	   $message = "Hej ".get_post_meta($post->ID, 'ticket_poster', true).",\nDin rapport '".$post->post_title."' har just uppdaterats.\n\nDu kan besöka den uppdaterade rapporten på: ".get_permalink( $post->ID )."\n\nMvh\nWordPress Sverige";
//     if ( $post_type = get_post_type_object( $post->post_type ) )    
//        wp_mail(get_post_meta($post->ID, 'ticket_poster_mail', true), "[WPS Trac] Rapport uppdaterad (#".$post->ID.")", $message);
//}
//add_action( 'transition_post_status', '_trac_NotifyAuthor', 10, 3 );

//**************************************************
// Send mail to user when a ticket is updated
//**************************************************
//function trac_authorEditNotification($post_id) {
//   $post = get_post($post_id);
//   $author = get_userdata($post->post_author);
//
//   if ($author->ID == '2') {
//	   $message = "Hej ".get_post_meta($post->ID, 'ticket_poster', true).",\nDin rapport '".$post->post_title."' har just uppdaterats.\n\nDu kan besöka den uppdaterade rapporten på: ".get_permalink( $post->ID )."\n\nMvh\nWordPress Sverige";
//	   wp_mail(get_post_meta($post->ID, 'ticket_poster_mail', true), "[WPS Trac] Rapport uppdaterad (#".$post->ID.")", $message);
//   } else {
//	   $message = "Hej ".$author->display_name.",\nDin rapport '".$post->post_title."' har just uppdaterats.\n\nDu kan besöka den uppdaterade rapporten på: ".get_permalink( $post->ID )."\n\nMvh\nWordPress Sverige";
//	   wp_mail($author->user_email, "[WPS Trac] Rapport uppdaterad (#".$post->ID.")", $message);
//   }
//}
//add_action('edit_post', 'trac_authorEditNotification');
?>